<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->id();
            $table->string('order_id', 50)->unique()->comment('订单号');
            $table->unsignedBigInteger('room_id')->comment('房间ID');
            $table->unsignedBigInteger('user_id')->comment('用户ID');
            $table->json('dishes')->comment('菜品详情');
            $table->enum('status', ['pending', 'confirmed', 'cancelled', 'completed'])->default('pending')->comment('订单状态');
            $table->enum('payment_status', ['unpaid', 'paid', 'refunded'])->default('unpaid')->comment('支付状态');
            $table->string('payment_method', 50)->nullable()->comment('支付方式');
            $table->timestamp('payment_time')->nullable()->comment('支付时间');
            $table->text('notes')->nullable()->comment('订单备注');
            $table->timestamps();
            
            $table->foreign('room_id')->references('id')->on('rooms')->onDelete('cascade');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            
            $table->index('order_id');
            $table->index('room_id');
            $table->index('user_id');
            $table->index('status');
            $table->index('payment_status');
            $table->index('created_at');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('orders');
    }
};